package com.huanrong.aop;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class LogAdvice
{
    private static final Logger logger = Logger.getLogger(new Throwable().getStackTrace()[0].getClassName());
    
    @Pointcut("execution(* com.huanrong.ajax..*(..))")
    public void ajaxPointcut()
    {
    
    }
    
    @Pointcut("execution(* com.huanrong.controller..*(..))")
    public void controllerPointcut()
    {
    
    }
    
    @Before(value = "(ajaxPointcut()||controllerPointcut())&& args(request,..)")
    public void beforeAdvice(HttpServletRequest request)
    {
        logger.info(request.getRequestURI() + "-->start");
    }
    
    @AfterReturning(value = "(ajaxPointcut()||controllerPointcut())&& args(request,..)")
    public void afterAdvice(HttpServletRequest request)
    {
        logger.info(request.getRequestURI() + "-->onEnd");
    }
    
    // 标注该方法体为异常通知，当目标方法出现异常时，执行该方法体
    @AfterThrowing(value = "ajaxPointcut()&& args(request,..)", throwing = "ex")
    public void logBillOptException(JoinPoint jp, HttpServletRequest request, Exception ex)
    {
    
    }
    
    /**
     * 记录log
     * 
     * @Description
     * @author congyue.lu
     * @param pjp
     * @return
     * @throws Throwable
     */
    @Around(value = "ajaxPointcut()&& args(request,..)")
    public Object optLog(ProceedingJoinPoint pjp, HttpServletRequest request)
        throws Throwable
    {
        Object returnValue = pjp.proceed();
        return returnValue;
    }
}
